﻿Imports Entidad
Imports Negocio
Public Class frmReporte

    Dim servicio1 As New DocumentoClienteNegocio
    Dim estado As String = ""

    Sub limpiarItems()
        If dgvItems.RowCount >= 1 Then
            For i As Integer = 0 To dgvItems.RowCount - 1
                dgvItems.Rows.Remove(dgvItems.CurrentRow)
            Next
        End If
    End Sub

    Sub limpiarBultos()
        If dgvBultos.RowCount >= 1 Then
            For i As Integer = 0 To dgvBultos.RowCount - 1
                dgvBultos.Rows.Remove(dgvBultos.CurrentRow)
            Next
        End If
    End Sub

    Sub limpiar()
        limpiarItems()
        limpiarBultos()
    End Sub

    Sub mostrarTotales()
        Dim t1 As Integer = 0
        Dim t2 As Decimal = 0.0
        Dim t3 As Decimal = 0.0
        For Each fila As DataGridViewRow In dgvBultos.Rows
            t1 = t1 + CInt(fila.Cells("Column22").Value)
            t2 = t2 + CInt(fila.Cells("Column23").Value)
            t3 = t3 + CInt(fila.Cells("Column24").Value)
        Next
        lblT1.Text = t1
        lblT2.Text = t2
        lblT3.Text = t3
    End Sub

    Sub listarBultos(pedido As String)
        Dim ds As DataSet = servicio1.listarBultosPedidos(pedido)

        dgvBultos.DataSource = ds.Tables(0)

        If ds.Tables(0).Rows.Count <> 0 Then
            dgvBultos.Rows(0).Selected = False
            mostrarTotales()
        Else
            lblT1.Text = "0"
            lblT2.Text = "0.0"
            lblT3.Text = "0.0"
        End If
    End Sub

    Sub listarDetallePedidos(pedido As String)
        Dim ds As DataSet = servicio1.listarItemsPedidos(pedido)

        dgvItems.DataSource = ds.Tables(0)

        If ds.Tables(0).Rows.Count <> 0 Then
            dgvItems.Rows(0).Selected = False
        End If
    End Sub

    Sub listarPedidosXEstado(est As String)
        Dim ds As DataSet = servicio1.listarPedidosXEstado(est)

        dgvPedidos.DataSource = ds.Tables(0)

        If ds.Tables(0).Rows.Count <> 0 Then
            dgvPedidos.Rows(0).Selected = False
        End If
    End Sub

    Sub listarEstados()
        Dim ds As DataSet = servicio1.listarEstadosPedidos()

        dgvEstados.DataSource = ds.Tables(0)

        If ds.Tables(0).Rows.Count <> 0 Then
            estado = dgvEstados.Item("Column1", 0).Value

            listarPedidosXEstado(estado)
        End If
    End Sub

    Sub iniciar()
        listarEstados()
    End Sub

    Private Sub frmReporte_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        iniciar()
    End Sub

    Private Sub dgvEstados_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvEstados.CellClick
        Dim f As Integer = e.RowIndex

        If f > -1 Then
            limpiar()
            lblT1.Text = "0"
            lblT2.Text = "0.0"
            lblT3.Text = "0.0"
            listarPedidosXEstado(dgvEstados.Item("Column1", f).Value)
        End If
    End Sub

    Private Sub dgvPedidos_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPedidos.CellClick
        Dim f As Integer = e.RowIndex

        If f > -1 Then
            listarDetallePedidos(dgvPedidos.Item("Column4", f).Value)
            listarBultos(dgvPedidos.Item("Column4", f).Value)
        End If
    End Sub

    Private Sub btnRefrescar_Click(sender As System.Object, e As System.EventArgs) Handles btnRefrescar.Click
        limpiar()
        listarEstados()
        lblT1.Text = "0"
        lblT2.Text = "0.0"
        lblT3.Text = "0.0"
    End Sub

End Class